package com.frostmourne.mxsguimaker.commands.sub;

import com.frostmourne.mxsguimaker.objects.GuiMakerMold;
import com.gitee.mxstarsex.objects.builders.bukkit.commands.CommandElement;
import com.gitee.mxstarsex.objects.builders.bukkit.commands.SubCommandBase;
import com.gitee.mxstarsex.utils.PlayerUtils;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ForceOpenCommand extends SubCommandBase {
    public ForceOpenCommand() {
        super("force-open", "server.admin");
        setAlias(
                new ArrayList<>(
                        Arrays.asList(
                                "fo","force",
                                "强制打开","f-o"
                        )
                )
        );
    }

    @Override
    public void run(CommandElement commandElement) {

        if (!commandElement.isPlayer()){
            return;
        }

        String[] args = commandElement.getArgs();
        if (args.length < 2){
            commandElement.sendMessage("&8[&6&l!&8]&f/" + commandElement.getDispatchCommand() + " force-open <背包名称> <玩家名称> &6强制打开玩家的背包(需在线)");
            return;
        }else{

            boolean isOp = commandElement.getSender().isOp();
            String inventoryName = args[1];
            String playerName = commandElement.isPlayer() ? commandElement.getPlayer().getName() : null;
            if (args.length > 2 && isOp){
                playerName = args[2];
            }
            if (playerName != null){

                Player player = Bukkit.getPlayer(playerName);
                if (PlayerUtils.playerIsNotNull(player)){
                    GuiMakerMold guiMakerMold = GuiMakerMold.getGuiMakerMold(inventoryName);
                    if (guiMakerMold != null){
                        guiMakerMold.open(player,commandElement.getPlayer());
                    }else{
                        commandElement.sendMessage("&8[&6&l!&8]&f不存在的背包名称");
                    }

                }else{
                    commandElement.sendMessage("&8[&6&l!&8]&f玩家不在线");
                }

            }else{
                if (isOp){
                    commandElement.sendMessage("&8[&6&l!&8]&f请输入玩家名称");
                }
            }

        }

    }

    @Override
    public List<String> tabExecute(CommandElement commandElement) {
        String[] args = commandElement.getArgs();
        List<String> strings = new ArrayList<>();
        boolean isOp = commandElement.getSender().isOp();
        String key;
        if (args.length == 2){
            key = args[1];
            for (String s : GuiMakerMold.GUI_MAKER_MOLD_MAP.keySet()){
                if (s.startsWith(key)){
                    strings.add(s);
                }
            }
        }
        if (args.length == 3 && isOp){
            key = args[2];
            for (Player player : Bukkit.getOnlinePlayers()){
                if (player.getName().startsWith(key)){
                    strings.add(player.getName());
                }
            }
        }
        return strings;
    }
}
